package leedcode_path.treeNoDE;

import java.util.LinkedList;
import java.util.Queue;

public class 填充每个节点的下一个右侧节点指针2 {
    /*
    * 填充每个节点的下一个右侧节点指针*/
    class Node {
        public int val;
        public Node left;
        public Node right;
        public Node next;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, Node _left, Node _right, Node _next) {
            val = _val;
            left = _left;
            right = _right;
            next = _next;
        }
    };
    public Node connect(Node root) {
        if (root==null) return root;
        Queue<Node> que= new LinkedList<Node>();
        que.offer(root);
        while (!que.isEmpty()){
            int len=que.size();
            for (int i = 0; i < len; i++) {
                Node cur=que.poll();
                if (i<len-1){
                    cur.next=que.peek();
                }
                if (cur.left!=null) que.offer(cur.left);
                if (cur.right!=null) que.offer(cur.right);

            }
        }
        return root;
    }
}
